package pl.edu.agh.hollonCommision.graph.metrics;

import java.util.List;
import java.util.Map;

import pl.edu.agh.hollonCommision.graph.Edge;
import pl.edu.agh.hollonCommision.graph.GraphModel;
import pl.edu.agh.hollonCommision.graph.Vertex;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseGraph;
import edu.uci.ics.jung.graph.util.Pair;

public class PageRank extends IGraphMetric {

	@Override
	public void calculateMetric(GraphModel g) {
		Map<Integer, Vertex> vertices = g.getVertices();
		Map<Integer, List<Edge>> edges = g.getEdges();
		Graph<Vertex, Edge> gr = new SparseGraph<Vertex, Edge>();
		for (Vertex v : vertices.values()) {
			gr.addVertex(v);
		}
		for (List<Edge> edg : edges.values()) {
			for (Edge e : edg) {
				gr.addEdge(e, new Pair<Vertex>(e.getV1(), e.getV2()));
			}
		}
		System.out.println("Ala");
		edu.uci.ics.jung.algorithms.scoring.PageRank<Vertex, Edge> page = new edu.uci.ics.jung.algorithms.scoring.PageRank<Vertex, Edge>(
				gr, 0.15);
		page.evaluate();
		for (Vertex v : vertices.values()) {
			v.setCurrentMetric("pageRank");
			v.setMetricValue("pageRank", page.getVertexScore(v));
		}
		normalizeMetric(g, "pageRank");
	}
}
